const mongoose = require('mongoose');
const manpowerScheam = require('../schema/manpower.js');
const db = require('../config/db.js');
const manPower = mongoose.model('manpower', manpowerScheam);
class ManPowerModel {
    //模型化人力表
    /**
     * 创建一条人力数据
     * @param data
     * @returns {Promise.<*>}
     */
    static async createManpower(data) {
        let example = new manPower(data);
        try {
            return await example.save()
        }
        catch (err) {
            console.log('人力保存失败');
            return false;
        }
    }
    /**
     * 删除一条人力数据
     * @param id
     * @returns {Promise.<boolean>}
     */
    static async deleteManpower(id) {
        let _id = { _id: mongoose.Types.ObjectId(id) };
        return manPower.findByIdAndRemove(_id);
    }
    /**
     * 更新一条人力数据
     * @param data
     * @returns {Promise.<boolean>}
     */
    static async updateManpower(data) {
        let conditions = { _id: mongoose.Types.ObjectId(data._id) };
            let setjson = {};
            (data.md_content !== undefined) && (setjson.md_content = data.md_content);
            (data.html_content !== undefined) && (setjson.html_content = data.html_content);
            (data.update_time !== undefined) && (setjson.update_time = data.update_time);
            (data.title !== undefined) && (setjson.title = data.title);

            let setupdate = { $set: setjson };
            return await manPower.findByIdAndUpdate(conditions, setupdate, {new:true});
    }
   
    /**
     * 查询人力数据
     * @param id
     * @returns {Promise.<*>}
     */
    static async searchManpower(data) {
        console.log(data)
        return manPower.find(data).limit(20).then(result => {
            return {
                list: result,
            }
        });
    }    
}
module.exports = ManPowerModel

